<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>2_对props进行限制</title>
    <!-- 引入 react核心库 -->
    <script type="text/javascript" src="../js/react.development.js"></script>
    <!-- 引入 react-dom 用于支持操作DOM -->
    <script type="text/javascript" src="../js/react-dom.development.js"></script>
    <!-- 引入 babel 用于将 jsx转为 js -->
    <script type="text/javascript" src="../js/babel.min.js"></script>
    <!-- 引入 prop-types.js 用于对组件标签属性进行限制 -->
    <script type="text/javascript" src="../js/prop-types.js"></script>
</head>
<body>
    <div id="test1"></div>
    <div id="test2"></div>
    <script type="text/babel">
        class Person extends React.Component {
            render() {
                // this.props.age = 10;         // 此处报错，props属性是只读的，不允许修改
                let {name, sex, age} = this.props;
                return (
                    <ul>
                        <li>姓名：{name}</li>
                        <li>性别：{sex}</li>
                        <li>年龄：{age+ 1}</li>
                    </ul>
                );
            }
        }

        // 对标签属性进行类型、必要性的限制
        Person.propTypes = {
            name: PropTypes.string.isRequired,      // 限制name必传，且为字符串
            sex: PropTypes.string,                  // 限制sex为字符串
            age: PropTypes.number,                  // 限制age为数值
            speak: PropTypes.func,                  // 限制speak为函数
        }

        // 默认值
        Person.defaultProps ={
            sex: '男',       // sex 默认值是 男
            age: 18         // age 默认值是 18
        }

        ReactDOM.render(<Person name={"tom"} age={19} speak={speak} />, document.getElementById("test1"));
        const p = {name: "jerry", age: 19, sex: '女'}
        ReactDOM.render(<Person {...p} />, document.getElementById("test2"));


        function speak() {
            console.log("我要说话");
        }

    </script>
</body>
</html>